package com.building.hcrm.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.building.hcrm.R;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseAsyncTask;
import com.building.hcrm.base.BaseParam;
import com.building.hcrm.base.BaseStaffSelector.SelectedStaffListener;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.ComFunc;
import com.building.hcrm.common.User;
import com.building.hcrm.model.PlanDetail;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.ui.customcontrols.ComStaffSelector;
import com.building.hcrm.wcf.TaskServiceProxy;

public class TaskLowListActivity extends BaseActivity
{
	private int iCount = 0;
	private ComStaffSelector _selector;
	private Spinner _spinStatus;
	private ListView _lstv;
	private List<PlanDetail> _lstPlan;// 完整数据
	private List<PlanDetail> _lstPlanFilter;// 任务状态筛选后数据

	private ImageButton _imgBtnTitleRight;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_task_low_list);
		InitTitle();
		_selector = (ComStaffSelector) findViewById(R.id.staff_selector);
		_selector.load(ComStaffSelector.DISPLAY_TYPE_ALL, User.getInstance(this));
		_lstv = (ListView) findViewById(R.id.task_record_list);
		_spinStatus = (Spinner) findViewById(R.id.spinner_task_status);
		_spinStatus.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, getResources()
				.getStringArray(R.array.task_status)));
		_selector.setOnSelectedStaffListener(selectedStaffListener);
		_spinStatus.setOnItemSelectedListener(spinnerSelectedListener);
		_lstv.setOnItemClickListener(listItemClickListener);
	}

	private OnItemClickListener listItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
		{
			if (arg2 >= 0)
			{
				Intent intent = null;
				intent = new Intent(getApplicationContext(), TaskStatusChangeActivity.class);
				intent.putExtra("PlanIns", _lstPlanFilter == null ? _lstPlan.get(arg2) : _lstPlanFilter.get(arg2));
				intent.putExtra(ComConst.INTENT_EXTRA_SYSUSER, _selector.getCurrentStaff());
				startActivity(intent);
			}
		}
	};

	private void InitTitle()
	{
		imgbtnHome = (ImageButton) findViewById(R.id.imgbtn_home);
		imgbtnHome.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		setTitle(getString(R.string.task_title));
		_imgBtnTitleRight = (ImageButton) findViewById(R.id.imgbtn_save);
		_imgBtnTitleRight.setVisibility(View.INVISIBLE);
	}

	private void DataBind()
	{
		SysUser sysUser = null;
		do
		{
			try
			{
				sysUser = _selector.getCurrentStaff();
			}
			catch (Exception e)
			{
				Log.e(LTAG, ComFunc.getExceptionMessage(e), e);
			}
		}
		while (null == sysUser && iCount < 99);

		BaseParam param = new BaseParam();
		param.userNum = sysUser.getUserNum();

		new BaseAsyncTask<BaseParam, String, List<PlanDetail>>(TaskLowListActivity.this)
		{
			@Override
			protected void onPreExecute()
			{
				super.onPreExecute();
			}

			@Override
			protected List<PlanDetail> doInBackground(BaseParam... params)
			{
				List<PlanDetail> re = null;
				if (null != params && params.length > 0)
				{
					try
					{
						re = TaskServiceProxy.getInstance(TaskLowListActivity.this).TaskSelfList(params[0]);
					}
					catch (Exception e)
					{
						Log.e(LTAG, ComFunc.getExceptionMessage(e), e);
					}
				}
				return re;
			}

			@Override
			protected void onPostExecute(List<PlanDetail> result)
			{
				super.onPostExecute(result);
				_lstPlan = result;
				setListView(filterPlan());
			}
		}.execute(param);
	}

	private void setListView(List<PlanDetail> param)
	{
		List<HashMap<String, Object>> lstHM = new ArrayList<HashMap<String, Object>>();
		if (null != param)
		{
			for (PlanDetail item : param)
			{
				HashMap<String, Object> hashMap = new HashMap<String, Object>();
				hashMap.put("strStart", ComFunc.GetStringByDate(item.PlanStartTime, ComConst.DATE_FORMAT));
				hashMap.put("strEnd", ComFunc.GetStringByDate(item.PlanEndTime, ComConst.DATE_FORMAT));
				hashMap.put("strStatus", item.PlanState);
				hashMap.put("strContent", item.PlanContent);
				lstHM.add(hashMap);
			}
		}
		SimpleAdapter sAdapter = new SimpleAdapter(this, lstHM, R.layout.list_item_task, new String[] { "strStart",
				"strEnd", "strStatus", "strContent" }, new int[] { R.id.txtStart, R.id.txtEnd, R.id.txtTaskStatus,
				R.id.txtContent });

		_lstv.setAdapter(sAdapter);
	}

	private List<PlanDetail> filterPlan()
	{
		List<PlanDetail> re = null;
		if (null != _spinStatus.getSelectedItem())
		{
			String strStatus = _spinStatus.getSelectedItem().toString();

			if (getString(R.string.task_status_all).equals(strStatus))
			{
				re = _lstPlan;
			}
			else
			{
				if (null == _lstPlanFilter)
					_lstPlanFilter = new ArrayList<PlanDetail>();
				else
					_lstPlanFilter.clear();

				for (PlanDetail plan : _lstPlan)
				{
					if (strStatus.equals(plan.PlanState.trim()))
					{
						_lstPlanFilter.add(plan);
					}
				}
				re = _lstPlanFilter;
			}
		}
		return re;
	}

	private SelectedStaffListener selectedStaffListener = new SelectedStaffListener()
	{
		@Override
		public void onSelectedStaff(SysUser inStaff)
		{
			if (null != inStaff)
			{
				DataBind();
			}
		}
	};

	private OnItemSelectedListener spinnerSelectedListener = new OnItemSelectedListener()
	{
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
		{
			setListView(filterPlan());
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0)
		{

		}
	};
}
